package celiasoft.itasalib.query.resultparser;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import celiasoft.itasalib.query.ResultsMetaInfo;
import celiasoft.itasalib.query.resultparser.W3CParsingHandler.ElementParser;
import celiasoft.itasalib.structures.Actor;
import celiasoft.itasalib.structures.DataElement;
import celiasoft.itasalib.structures.Show;
import celiasoft.itasalib.structures.ShowExtraData;

public class QuerySingleShowResultParser extends IQueryResultParser {

	@SuppressWarnings("unused")
	private static final String TAG = "itasalib.parser.QueryAllShowResultPaser";

	public List<DataElement> parse(Document doc, ResultsMetaInfo meta) {
		
		NodeList data =  doc.getElementsByTagName("show");
		ArrayList<DataElement> res = new ArrayList<DataElement>();
		
		if(data.getLength() < 1 
				|| !data.item(0).hasChildNodes()){
			return res;
		}
		
		Node show = data.item(0);
		
		DataElement el = parseNode(show);
		if(el != null){
			res.add(el);	
		}
		
		return res;
	}

	private DataElement parseNode(Node n) {
		
		Element e = (Element)n;
		
		// base
		int id = W3CParsingHandler.parseIntValue(e, "id");
		String name = DomHandler.getValue(e, "name");
		int fans = W3CParsingHandler.parseIntValue(e, "fans");
		
		Date start = null , end = null;
		
		start = W3CParsingHandler.parseSimpleDateValue(e, "started");
		end = W3CParsingHandler.parseSimpleDateValue(e,"ended");
		
		Show el = new Show(id, name);
		el.setFans(fans);
		el.setStart(start);
		el.setEnd(end);
		
		// extra
		ShowExtraData extra = new ShowExtraData();
		
		extra.setIdTvdb(W3CParsingHandler.parseIntValue(e, "id_tvdb"));
		extra.setIdTvrage(W3CParsingHandler.parseIntValue(e, "id_tvrage"));
		extra.setNameTvdb(DomHandler.getValue(e, "name_tvdb"));
		extra.setNameTvrage(DomHandler.getValue(e, "name_tvrage"));
		
		extra.setPlot(DomHandler.getValue(e, "plot"));
		extra.setPlotLang(DomHandler.getValue(e, "plot_lang"));
		
		extra.setBanner(W3CParsingHandler.parseURLValue(e, "banner"));
		extra.setLastUpdate(W3CParsingHandler.parseW3CDateValue(e, "last_update"));
		extra.setFolderThumb(W3CParsingHandler.parseURLValue(e, "folder_thumb"));
		
		extra.setActors(W3CParsingHandler.parseList(e, "actors", new ActorParser()));
		
		extra.setGenres(DomHandler.getValue(e, "genres"));
		extra.setSeason(W3CParsingHandler.parseIntValue(e, "seasons"));
		extra.setClassification(DomHandler.getValue(e, "classification"));
		extra.setCountry(DomHandler.getValue(e, "country"));
		extra.setStatus(DomHandler.getValue(e, "status"));
		extra.setNetwork(DomHandler.getValue(e, "network"));
		
		extra.setRuntime(W3CParsingHandler.parseIntValue(e, "runtime"));
		extra.setAirday(W3CParsingHandler.parseStringDayValue(e, "airday"));
		extra.setAirtime(W3CParsingHandler.parseTime24hValue(e, "airtime"));
		
		extra.setLastEpNum(W3CParsingHandler.parseEpisodeDataValue(e, "lastep_num"));
		extra.setLastEpTitle(DomHandler.getValue(e,"lastep_title"));
		extra.setLastEpDate(W3CParsingHandler.parseSimpleDateValue(e, "lastep_date"));
		
		extra.setNextEpNum(W3CParsingHandler.parseEpisodeDataValue(e, "nextep_num"));
		extra.setNextEpTitle(DomHandler.getValue(e,"nextep_title"));
		extra.setNextEpDate(W3CParsingHandler.parseSimpleDateValue(e, "nextep_date"));
		
		el.setExtraShowData(extra);
		
		return el;
	}

	
	
	class ActorParser implements ElementParser<Actor>{

		public Actor parseElement(Node n) {
			Element e = (Element)n;
			
			String name = DomHandler.getValue(e, "name");
			String as = DomHandler.getValue(e, "as");
			URL image = W3CParsingHandler.parseURLValue(e, "image");
			
			
			Actor el = new Actor();
			el.setName(name);
			el.setAs(as);
			el.setImage(image);
			
			return el;
		}
		
	}
	
	
}


































